﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#include "BurnToolCommon/StdAfx.h"
#include "YModem.h"

namespace {
    const int DISPALCEMENT_SIZE_EIGHT = 8;
}

YModemClass::YModem::YModem()
{
}

YModemClass::YModem::~YModem()
{
}

unsigned short YModemClass::YModem::GetCrc(const unsigned char *buf, unsigned int len)
{
    const byte *inPtr = nullptr;
    inPtr = buf;
    unsigned short cksum = 0;
    for (int stat = len; stat > 0; stat--) {
        cksum = cksum ^ (static_cast<uint>(*inPtr++) << DISPALCEMENT_SIZE_EIGHT);
        for (int i = 8; i != 0; i--) {
            if ((cksum & 0x8000) != 0) {
                cksum = (cksum << 1) ^ 0x1021;
            } else {
                cksum = cksum << 1;
            }
        }
    }
    return cksum;
}
